package com.zhao.tongcheng.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;


public class FileUtil {
	static void p(Object s)
	{
		System.out.println(s);//
	}
	public static void read1()
	{
		File f=new File("c:\\windows\\iProInstLog.txt");
		p(f.getName());//
		//p(f.getTotalSpace());
		
		try {
			InputStream is=new FileInputStream(f);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
/**
 * 
 * 例如：
char[] buf=new char[1000];
调用rd. read(buf,0,140)读取140个字符放入buf中（从0开始）。
若要以字节为单位读，就转成字节流，同样方法读。 
 * **/
	public static void read2(String fUrl)
	{
		
		File f=null;
		RandomAccessFile raf=null;
		try {
			f=new File(fUrl);
			 raf=new RandomAccessFile(f,"r");
			long size=raf.length();
			
			p(size);
			int pageSize=3;//每页显示字节数
			int i=0;//读取标记
			
			while (i<size)
			{
				
				byte[] bytes = new byte[ pageSize];
			
				raf.seek(i);				
				raf.read(bytes,0,pageSize);			
				p(i+"---"+new String(bytes));
				i=i+pageSize;				
			}

			int pageNum=0;
			
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try {
				if (raf!=null)
					raf.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void write(String file,String fileVaule)
	{
		File f=new File(file);
		try {

			Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); 
			writer.write(fileVaule); 
			
			writer.close();  


			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String args[])
	{
		String url=":\\andriod\\docs\\sitemap.tx";
		url="d:\\hello.txt";
		read2(url);
	}
}
